<?php
defined('IN_TS') or die('Access Denied.');

switch($ts){

	
	case "base":
		$groupid = $_GET['groupid'];
		if($groupid){
			$arrGroup=aac('group')->find('group',array('groupid'=>$groupid));
			$jointype = $_GET['jointype'];
			if($jointype==''){
				$jointype=1;
			}
			if($jointype==1){
				$tablename = '馆员';
			}
			if($jointype==2){
				$tablename = '馆东';
				$donate = $_GET['donate'];
				$lishi = $_GET['lishi'];
				$isvo = $_GET['isvo'];
				$isdonate = $_GET['isdonate'];
			}
			$title = $arrGroup['groupname'].$tablename.'报名表';
			$strTopic = aac('group')->find('group_topic',array(
				'topicid'=>224,
			));
			$strTopic1 = aac('group')->find('group_topic',array(
				'topicid'=>280,
			));
			$strTopic['content'] = tsDecode($strTopic['content'],$tp);
			$strTopic1['content'] = tsDecode($strTopic1['content'],$tp);
			include template("join");
		}else{
			$title = '基本设置';
			include template("setting_base");
		}

		break;
		
	case "basedo":

		$username = t($_POST['username']);
		$comefrom = t($_POST['comefrom']);
		$phone = t($_POST['phone']);
		$how = t($_POST['how']);
		$about = t($_POST['about']);
		$sex = t($_POST['sex']);
		$birth = strtotime($_POST['birth']);
		$province = t($_POST['province']);
		$city = t($_POST['city']);
		$orgid = t($_POST['orgid']);
		$leadership = t($_POST['leadership']);
		$groupid = t($_POST['groupid']);
		$jointype = t($_POST['jointype']);
		$isdonate =  t($_POST['isdonate']);
		$idcard =  t($_POST['idcard']);
		$link = t($_POST['link']);
		$strUser = aac('user')->find('user_info',array('userid'=>$userid));
		if($jointype == 2){
			$isvo = t($_POST['isvo']);
			$donate = t($_POST['donate']);
		}



		$skill='';
		for($i=0;$i<=3;$i++){
			if($_POST['skill'][$i]!=''){
				if($skill==''){
					$skill=$_POST['skill'][$i];
				}else{
					$skill=$skill.','.$_POST['skill'][$i];
				}
			}
		}

		if($TS_USER == '') {
			tsNotice("机房重地，闲人免进！");
		
		}
		if($username == '') {
		tsNotice("不管做什么都需要有一个名号吧^_^");
	}

		if(strlen($username) < 4 || strlen($username) > 20) {
			tsNotice("名称长度必须在4到20字符之间!");
		}
		
		if($username != $strUser['username']){
			if($TS_APP['banuser']){
				$arrUserName = explode('|',$TS_APP['banuser']);
				if(in_array($username,$arrUserName)){
					tsNotice("该名称被系统禁用，请换一个！");
				}
			}
			/*
			$isUserName = $new['my']->findCount('user_info',array(
				'username'=>$username,
			));
			
			if($isUserName > 0) {
			
				tsNotice("成员名已经存在，请换个成员名！");
			
			}*/
		}


		if($link){
			follow($link);
			$follower = aac('user')->find('user_info',array('userid'=>$link));
			$followText = '，是<a href="'.tsUrl('user','followed',array('id'=>$link)).'">'.$follower['username'].'</a>把微澜推荐给ta的';
		}

		if($jointype==1){
			if($comefrom == '') {
				tsNotice("请填写身份");
			}

			if($orgid == '') {
				tsNotice("请填写所属组织");
			}
		}

		if($strUser['isvo']==1||$jointype==1){
			if($idcard == '') {
				tsNotice("请填写身份证号或护照号");
			}
		}


		if($phone == '') {
			tsNotice("手机号必填，放心，只有管理员可以看见^_^");
		}
		if($how == '') {
			tsNotice("告诉我们你怎么知道微澜的吧^_^");
		}

		if($about == '') {
			tsNotice("请写个自我介绍吧^_^");
		}

		/*
		if(intval($TS_USER['isadmin'])==0){
			//过滤内容开始
			aac('system')->antiWord($username);
			aac('system')->antiWord($phone);
			//aac('system')->antiWord($blog);
			aac('system')->antiWord($about);
			//过滤内容结束
		}
*/

        //签名中禁止写URL,Email
        /*
        if(filter_var($signed, FILTER_SANITIZE_URL) || filter_var($signed, FILTER_VALIDATE_EMAIL)){
            tsNotice('签名不合法！请修改后再提交！');
        }

        if(filter_var($about, FILTER_SANITIZE_URL) || filter_var($about, FILTER_VALIDATE_EMAIL)){
            tsNotice('个人介绍不合法！请修改后再提交！');
        }
        */

		$arr =array(
			'username' => $username,
			'sex'	=> $sex,
			'phone'	=> $phone,
			'how' => $how,
			'about' => $about,
			'uptime'=>time(),
		);
		if($idcard){
			$arr['idcard'] = $idcard;
		}
		if($comefrom){
			$arr['comefrom'] = $comefrom;
		}
		if($skill){
			$arr['skill'] = $skill;
		}
		if($orgid){
			$arr['orgid'] = $orgid;
		}
		if($TS_USER['count_duty']==0){
			$arr['addtime'] = time();
		}
		if($leadership){
			if($strUser['leadership']<>$leadership&&$strUser['leadership']<>3){
				$leadership = 3;
			}
			$arr['leadership'] = $leadership;
		}

		if($jointype){
			if($jointype==1){
				$arr['isvo']=1;
			}else{
				$arr['isstock']=-1;
				if($isvo){
					$arr['isvo']=1;
				}
			}
		}


		if($birth){
			$arr['birth'] = $birth;
		}

		if($province){
			$arr['province'] = $province;
		}
		if($city){
			$arr['city'] = $city;
		}
		//更新数据
		$new['my']->update('user_info',array(
			'userid'=>$userid,
		),$arr);

		$_SESSION['tsuser']['username']=$username;

		if($groupid) {
			$arrGroup = aac('group')->getOneGroup($groupid);
			$groupUserNum = aac('group')->find('group_user', array(
				'userid' => $userid,
				'groupid' => $groupid,
			));
			if ($groupUserNum) {
				tsNotice('报名完毕！');
				} else {


				$arr1 = array(
					'userid' => $TS_USER['userid'],
					'groupid' => $groupid,
					'addtime' => time(),
				);
				if ($jointype == 1) {
					$arr1['isvo'] = 1;
				}
				if ($jointype == 2) {
					$arr1['isstock'] = -1;
					if ($isvo == 1) {
						$arr1['isvo'] = 1;
					}
				}
				if($leadership){
					$arr1['leadership']=$leadership;
				}

				aac('group')->create('group_user', $arr1);

				$groupids = getGroupIds($userid);
				$count_group = aac('group')->findCount('group_user', array(
					'userid' => $userid,
				));

				aac('user')->update('user_info', array('userid' => $userid), array('count_group' => $count_group, 'groupids' => $groupids));

				//计算小组会员数
				$count_user = aac('group')->findCount('group_user', array(
					'groupid' => $groupid,
				));
				aac('group')->update('group', array(
					'groupid' => $groupid,
				), array(
					'count_user' => $count_user
				));
/*

				//给管理员发微信通知
				$arrAdmin = aac('user')->findAll('user_info', array('isadmin' => 1));
				foreach ($arrAdmin as $v) {
					if ($v['wxopenid']) {
						$arr = array(
							'group' => $arrGroup['groupname'],
							'username' => $username,
						);
						aac('wechat')->sendwechat(7, $v['userid'], $arr, tsUrl('user', 'duty', array('id' => $userid)));
					}
				}
*/
				//在社区发动态
				if ($jointype == 1) {
					$content = '报名做<a href="' . tsUrl('group', 'show', array('id' => $groupid)) . '">'. $arrGroup['groupname'] . '</a>馆员了'.$followText.'！';
					aac('weibo')->create('weibo', array(
						'userid' => $TS_USER['userid'],
						'groupid' => $groupid,
						'content' => $content,
						'isaudit' => 0,
						'addtime' => date('Y-m-d H:i:s'),
						'uptime' => date('Y-m-d H:i:s'),
					));
					if($_SESSION['tsuser']['follow_uid']) {
						//发送系统消息开始
						$msg_userid = '0';
						$msg_touserid = $_SESSION['tsuser']['follow_uid'];
						$msg_content = '你的朋友'.$_SESSION['tsuser']['username'].'报名做<a href="'.tsUrl('group','show',array('id'=>$groupid)).'">'.$arrGroup['groupname'].'</a>馆员了，ta是通过你的推荐了解微澜的，到目前为止，已经有'.$follower['count_followed'].'人通过你的转发来到了微澜，你干得漂亮，加油啊！';
						$msg_tourl = tsUrl('user', 'followed', array('id' => $_SESSION['tsuser']['follow_uid']));
						aac('message')->sendmsg($msg_userid, $msg_touserid, $msg_content, $msg_tourl);
						//发送系统消息end
					}
					if ($arrGroup['record']) {
						$url = $arrGroup['record'];
					} else {
						$url = tsUrl('group', 'newvo',array('id'=>$groupid));
					}
					$text = '报名成功，即将前往欢迎页面，请仔细阅读欢迎信息，按照提示做后续操作！';
				} else {
					$num = aac('group')->findCount('group_user', 'groupid=' . $groupid . ' and (isstock=1 or isstock=2)');
					$content = '报名做<a href="' . tsUrl('group', 'show', array('id' => $groupid)) . '">' . $arrGroup['groupname'] . '</a>馆东了' . $followText . '！';
					aac('weibo')->create('weibo', array(
						'userid' => $TS_USER['userid'],
						'groupid' => $groupid,
						'content' => $content,
						'isaudit' => 0,
						'addtime' => date('Y-m-d H:i:s'),
						'uptime' => date('Y-m-d H:i:s'),
					));

					if ($_SESSION['tsuser']['follow_uid']) {
						//发送系统消息开始
						$msg_userid = '0';
						$msg_touserid = $_SESSION['tsuser']['follow_uid'];
						$msg_content = '你的朋友' . $_SESSION['tsuser']['username'] . '报名做<a href="' . tsUrl('group', 'show', array('id' => $groupid)) . '">' . $arrGroup['groupname'] . '</a>馆东了，ta是通过你的推荐了解微澜的，到目前为止，已经有' . $follower['count_followed'] . '人通过你的转发来到了微澜，你干得漂亮，加油啊！';
						$msg_tourl = tsUrl('user', 'followed', array('id' => $_SESSION['tsuser']['follow_uid']));
						aac('message')->sendmsg($msg_userid, $msg_touserid, $msg_content, $msg_tourl);
						//发送系统消息end
					}
					if ($arrGroup['donateurl']) {
						if ($isdonate == 1) {
							$url = tsUrl('group', 'newstock', array('groupid' => $groupid));

						} else {
							$url = $arrGroup['donateurl'];
						}
					} else {
						$url = 'http://lxi.me/c03pr';
					}
					if ($groupid == 1) {
						$text = '请加油！接下来即将跳转到月捐页面，完成月捐才可以成为馆东。完成月捐后，请加微信xingongmin2007，发送口令"总馆馆东"。';
					} else {
						$text = $arrGroup['groupname'] . '已经有' . $num . '位馆东，';
						if ($num < 20) {
							$text = $text . '还差' . (20 - $num) . '人就可以达到举行馆东大会的要求了,';
						} else {
							$text = $text . '馆东越多，图书馆运营经费越充足，';
						}
						$text = $text . '请加油！接下来即将跳转到月捐页面，完成月捐才可以成为馆东。注意：完成月捐后，将出现提示页面，请扫客服二维码加微信，并发口令加群。';
					}
				}
				if ($url) {
					tsNotice($text, '点击前往', $url, 'true');
				} else {
					tsNotice("出错，请微信加xingongmin2007，向客服求助！");
				}
			}

		}else{
			tsNotice("基本资料更新成功！");
		}
	
		break;
		
	case "flash":
		$title = 'Flash上传头像';
		include template('setting_flash');
		break;
		
	case "cut":
		$title = '裁切头像';
		include template('setting_cut');
		break;
		
	case "cutdo":
		require_once 'thinksaas/tsImage.php';
		$resizeimage = new tsImage("uploadfile/user/".$strUser['face'], 190, 190, 2,"uploadfile/user/".$strUser['face']);
		tsDimg($strUser['face'],'user','120','120',$strUser['path']);
		header('Location: '.tsUrl('my','setting',array('ts'=>'face')));
		break;
		
	case "face":
		$uid = $_GET['uid'];
		if($uid){
			if($TS_USER ['isadmin'] == 0){
				tsNotice('没有操作权限！');
			}
			$userid = $uid;
		}
		$title = '头像设置';
		$arrFace = tsScanDir('uploadfile/user/face',1);
		include template("setting_face");
		break;
	//执行上传头像
	case "facedo":
		$uid = $_POST['uid'];
		if($uid){
			$userid = $uid;
		}
		if($_FILES['picfile']){
			//上传
			$arrUpload = tsUpload($_FILES['picfile'],$userid,'user',array('jpg','gif','png'));
			if($arrUpload){
				$new['my']->update('user_info',array(
					'userid'=>$userid,
				),array(
					'path'=>$arrUpload['path'],
					'face'=>$arrUpload['url'],
				));
				$filesize=abs(filesize('uploadfile/user/'.$arrUpload['url']));
				if($filesize<=0){
					$new['my']->update('user_info',array(
						'userid'=>$userid,
					),array(
						'path'=>'',
						'face'=>'',
					));
					tsNotice('上传头像失败，你可以使用系统默认头像！');
				}else{
					//更新缓存头像
					if($uid){
						tsDimg($arrUpload['url'],'user','120','120',$arrUpload['path']);
						header('Location: '.tsUrl('user','duty',array('userid'=>$uid)));
					}else{
						$_SESSION['tsuser']['face'] = $arrUpload['url'];
						$_SESSION['tsuser']['path'] = $arrUpload['path'];
						tsDimg($arrUpload['url'],'user','120','120',$arrUpload['path']);
						header('Location: '.tsUrl('my','setting',array('ts'=>'face')));
					}

				}
			}else{
				tsNotice('头像修改失败');
			}
		}
		
		break;
	
	//设置密码
	case "pwd":
		$title = '密码修改';
		include template("setting_pwd");
		break;
		
	case "pwddo":
		$theUser = $new['my']->find('user',array(
			'userid'=>$strUser['userid'],
		));
		
		//$oldpwd = trim($_POST['oldpwd']);
		$newpwd = trim($_POST['newpwd']);
		$renewpwd = trim($_POST['renewpwd']);
		if($newpwd=='' || $renewpwd=='') tsNotice("所有项都不能为空！");
		if($newpwd != $renewpwd) tsNotice('两次输入密码不一样！');
		
		//更新密码
		//if(md5($theUser['salt'].$oldpwd) != $theUser['pwd']) tsNotice("旧密码输入有误！");
		$salt = md5(rand());
		$new['my']->update('user',array(
			'userid'=>$strUser['userid'],
		),array(
			'pwd'=>md5($salt.$newpwd),
			'salt'=>$salt,
		));
		 tsNotice("密码设置成功！");
	
		break;
		
	//修改登录Email
	case "email":
		$title = '修改登录Email';
		include template('setting_email');
		break;
		
	case "emaildo":
		$email = trim($_POST['email']);
		if($email=='') tsNotice('Email不能为空！');
		if(valid_email($email) == false) tsNotice('Email输入有误！');
		if($email != $strUser['email']){
			$emailNum = $new['my']->findCount('user',array(
				'email'=>$email,
			));
			
			if($emailNum > 0) tsNotice("Email帐号已经存在，请换个其他Email帐号！");
			
			//更新Email
			$new['my']->update('user',array(
				'userid'=>$strUser['userid'],
			),array(
				'email'=>$email,
			));
			
			//修改信息并将成员设为未验证状态
			$new['my']->update('user_info',array(
				'userid'=>$strUser['userid'],
			),array(
				'email'=>$email,
				'isverify'=>'0',
			));
			
			tsNotice('Email帐号修改成功，下次请用'.$email.'登录网站！');
			
		}else{
			tsNotice('新Email帐号不能和旧Email帐号一样！');
		}
	
		break;
		
	//设置常居地 
	case "city":
		
		$title = '常居地修改';
		include template("setting_city");
		break;
		
	case "citydo":
	
		$province = trim($_POST['province']);
		$city = trim($_POST['city']);

		$new['my']->update('user_info',array(
			'userid'=>$userid,
		),array(
			'province'=>$province,
			'city'=>$city,
		));

		tsNotice("常居地更新成功！");
	
		break;
	
	//个人标签
	case "tag":
	
		$arrTag = aac('tag')->getObjTagByObjid('user','userid',$userid);
	
		$title = '个人标签修改';
		include template("setting_tag");
		break;
		
	case "tagdo":
		break;

}